//
//  UIUtil.m
//  ScholarsBridge
//
//  Created by 李亚琪 on 2016/11/25.
//  Copyright © 2016年 mingthink. All rights reserved.
//

#import "UIUtil.h"

@implementation UIUtil

+ (UIWindow *)getKeyWindow
{
    UIWindow *keywindow = [UIApplication sharedApplication].keyWindow;
    
    return keywindow;
    
}


+(NSInteger)getStatusBarHeight{
    CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
    return statusBarFrame.size.height;
}

+(UIFont *)getCustomSystemFont:(CGFloat)size {
    return [UIFont systemFontOfSize:size];//[UIFont fontWithName:@"FZLTKHK--GBK1-0" size:size];
}

+ (UIColor *)getColor:(NSString *)hexColor {
    NSString *cString = [[hexColor stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    // String should be 6 or 8 characters
    if ([cString length] < 6) {
        return [UIColor clearColor];
    }
    // strip 0X if it appears
    if ([cString hasPrefix:@"0X"])
        cString = [cString substringFromIndex:2];
    if ([cString hasPrefix:@"#"])
        cString = [cString substringFromIndex:1];
    if ([cString length] != 6)
        return [UIColor clearColor];
    // Separate into r, g, b substrings
    NSRange range;
    range.location = 0;
    range.length = 2;
    //r
    NSString *rString = [cString substringWithRange:range];
    //g
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    //b
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    
    // Scan values
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    return [UIColor colorWithRed:((float) r / 255.0f) green:((float) g / 255.0f) blue:((float) b / 255.0f) alpha:1.0f];
}

+(void)showEmptyOrNetworkError:(UIView *)targetView x:(float)x offsetY:(float)offsetY isNetWorkError:(BOOL)isNetWorkError delegate:(id)delegateVC newViewTag:(NSInteger)viewTag
{
    if ([targetView viewWithTag:viewTag]) {
        return;
    }
    
    UIView *centerView = [[UIView alloc]initWithFrame:CGRectMake(x, 0, targetView.frame.size.width, targetView.frame.size.height)];
    [centerView setBackgroundColor:[UIColor whiteColor]];
    [centerView setTag:viewTag];
    [targetView addSubview:centerView];
    [targetView bringSubviewToFront:centerView];
    
    UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(0,0, 100, 140)];
    img.contentMode = UIViewContentModeScaleAspectFill;
    [centerView addSubview:img];
    [img setImage:[UIImage imageNamed:isNetWorkError ? @"networkError":@"emptyData"]];
    img.center = CGPointMake(centerView.center.x - x, centerView.center.y + offsetY);
    
    UILabel *label2 = UILabel.new;
    [centerView addSubview:label2];
    label2.text = isNetWorkError ? @"亲,可能您手机网络不顺畅":@"亲,该页面暂无内容";
    label2.textAlignment = NSTextAlignmentCenter;
    label2.font = [UIUtil getCustomSystemFont:18.0];
    //label2.textColor = ;
    label2.frame = CGRectMake(0, CGRectGetMaxY(img.frame)+20, targetView.frame.size.width, 30);
    
    UILabel *label3 = UILabel.new;
    [centerView addSubview:label3];
    label3.text = isNetWorkError ? @"请检查手机是否联网":@"快去其他页面看看吧";
    label3.textAlignment = NSTextAlignmentCenter;
    label3.font = [UIUtil getCustomSystemFont:14.0];
    //label3.textColor = LightGray;
    label3.frame = CGRectMake(0, CGRectGetMaxY(label2.frame), targetView.frame.size.width, 30);
    
    UIButton *exampleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [centerView addSubview:exampleBtn];
    [exampleBtn setTitle:isNetWorkError ? @"重新加载" : @"重新加载" forState:UIControlStateNormal];
    exampleBtn.titleLabel.font = [UIUtil getCustomSystemFont:14.0];
    [exampleBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    exampleBtn.layer.borderWidth = 1;
    exampleBtn.layer.cornerRadius = 8.0f;
    exampleBtn.tag = isNetWorkError;
    //exampleBtn.layer.borderColor = LightGray.CGColor;
    [exampleBtn addTarget:delegateVC action:@selector(errorButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    exampleBtn.frame = CGRectMake((CGRectGetWidth(centerView.frame)-120)/2.0, CGRectGetMaxY(label3.frame)+15, 120, 40);
}


+(void)showNeedLoginType:(LoginTypeTips)type superView:(UIView *)targetView x:(float)x offsetY:(float)offsetY delegate:(id)delegateVC newViewTag:(NSInteger)viewTag
{
    if ([targetView viewWithTag:viewTag]) {
        return;
    }
    
    NSArray *aryTips = @[@"您还未登录,请先登录后查看",@"您还未绑定,请先绑定后查看"];
    NSArray *aryBtnTips = @[@"去登录",@"去绑定"];
    
    UIView *centerView = [[UIView alloc]initWithFrame:CGRectMake(x, 0, targetView.frame.size.width, targetView.frame.size.height)];
    [centerView setBackgroundColor:[UIColor whiteColor]];
    [centerView setTag:viewTag];
    [targetView addSubview:centerView];
    [targetView bringSubviewToFront:centerView];
    
    UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(0,0, 100, 140)];
    img.contentMode = UIViewContentModeScaleAspectFill;
    [centerView addSubview:img];
    [img setImage:[UIImage imageNamed:@"needLogin"]];
    img.center = CGPointMake(centerView.center.x - x, centerView.center.y + offsetY);
    
    UILabel *label2 = UILabel.new;
    [centerView addSubview:label2];
    label2.text = aryTips[type];
    label2.textAlignment = NSTextAlignmentCenter;
    label2.font = [UIUtil getCustomSystemFont:16.0];
    //label2.textColor = GrayColor;
    label2.frame = CGRectMake(0, CGRectGetMaxY(img.frame)+15, targetView.frame.size.width, 30);
    
    UIButton *exampleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [centerView addSubview:exampleBtn];
    [exampleBtn setTitle:aryBtnTips[type] forState:UIControlStateNormal];
    exampleBtn.titleLabel.font = [UIUtil getCustomSystemFont:14.0];
    [exampleBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    exampleBtn.layer.borderWidth = 1;
    exampleBtn.layer.cornerRadius = 8.0f;
    exampleBtn.tag = type;
    //exampleBtn.layer.borderColor = GrayColor.CGColor;
    [exampleBtn addTarget:delegateVC action:@selector(LoginTypeTipsClick:) forControlEvents:UIControlEventTouchUpInside];
    exampleBtn.frame = CGRectMake((CGRectGetWidth(centerView.frame)-120)/2.0, CGRectGetMaxY(label2.frame)+15, 120, 40);
}

+(float)getWidthWith:(NSDictionary *)attributes height:(float)textHeight text:(NSString *)text{
    //UILineBreakModeCharacterWrap
    return [text boundingRectWithSize:CGSizeMake(CGFLOAT_MAX,textHeight) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size.width;
}

+(float)getHeightWith:(NSDictionary *)attributes width:(float)width text:(NSString *)text{
    return [text boundingRectWithSize:CGSizeMake(width,CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size.height;
}

+ (UIImage *)createImageWithColor:(UIColor *)color size:(CGSize)size {
    if (!color || size.width <= 0 || size.height <= 0) return nil;
    CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, color.CGColor);
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}
//获取当前屏幕显示的viewcontroller
+ (UIViewController *)getCurrentVC
{
    UIViewController *result = nil;
    
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    if (window.windowLevel != UIWindowLevelNormal)
    {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * tmpWin in windows)
        {
            if (tmpWin.windowLevel == UIWindowLevelNormal)
            {
                window = tmpWin;
                break;
            }
        }
    }
    
    UIView *frontView = [[window subviews] objectAtIndex:0];
    id nextResponder = [frontView nextResponder];
    
    if ([nextResponder isKindOfClass:[UIViewController class]])
        result = nextResponder;
    else
        result = window.rootViewController;
    
    return result;
}
+(void)showLoadingProgress:(NSString *)title{
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeClear];//背景不能点击
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
    [SVProgressHUD setCornerRadius:6];
    [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeFlat];//圆圈图形
    [SVProgressHUD showWithStatus:title];
}
+(void)closeLoadingProgress{
    [SVProgressHUD dismiss];
}
@end
